﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using Sharp.DataTransferObject;
using Sharp.Domain.PlatformBoundedContext.MasterDataModule.Aggregates;

using Sharp.Infrastructure;
using Sharp.Infrastructure.Crosscutting;
using Sharp.Infrastructure.Search;

namespace Sharp.Application.Platform
{

    /// <summary>
    /// 系统支持服务
    /// </summary>
    public partial class PlatformServiceImpl
    {
        #region 主数据

        #region 保存主数据

        /// <summary>
        /// 保存主数据
        /// </summary>
        /// <param name="masterData">主数据</param>
        /// <returns>操作结果</returns>
        [Logging("保存主数据“{@p0.Name}”"), CacheRemove]
        public Result SaveMasterData(MasterDataDTO masterData)
        {
            Result res = new Result();
            var info = masterData.To<MasterData>();
            if (masterData.Id.IsEmpty())
            {
                _masterDataRepository.Add(info);
            }
            else
            {
                _masterDataRepository.AutoUpdateFields(info).Update(info);
            }
            Context.Commit();

            return res;
        }

        #endregion

        #region 删除主数据

        /// <summary>
        /// 删除主数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Logging("删除主数据“{@p0}”"), CacheRemove]
        public Result DeleteMasterData(Guid[] ids)
        {
            var res = new Result();
            if (ids == null)
            {
                res.Err = "Id参数不可为空！";
                return res;
            }

            res = _masterDataService.DeleteMasterData(ids);

            return res;
        }

        #endregion

        #region 获取一条主数据

        /// <summary>
        /// 获取一条主数据
        /// </summary>
        /// <returns></returns>
        public MasterDataDTO GetMasterDataInfo(Guid id)
        {
            return _masterDataRepository.Get(id).To<MasterDataDTO>();
        }

        #endregion

        #region 获取主数据

        /// <summary>
        /// 获取主数据
        /// </summary>
        /// <returns></returns>
        [Cache]
        public IList<MasterDataSimpleDTO> GetMasterData()

        {
            var data = _masterDataRepository.GetList(null, "Category ASC,Code ASC", null, new string[] { })
                .To<List<MasterDataSimpleDTO>>();

            return data;
        }
        /// <summary>
        /// 获取主数据
        /// </summary>
        /// <param name="category">分类</param>
        /// <returns></returns>
        [Cache()]
        public IList<MasterDataSimpleDTO> GetMasterDataByCategory(string category)
        {
            var data = _masterDataRepository.GetList(x => x.Category == category, "Code ASC", null, new string[] { })
                .To<List<MasterDataSimpleDTO>>();

            return data;
        }
        #endregion

        #region 代码项翻译成文字项

        /// <summary>
        /// 代码项翻译成文字项
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string Code2Text(string code)
        {
            return _masterDataService.Code2Text(code);
        }

        #endregion

        #region 获取主数据分类

        /// <summary>
        /// 获取主数据分类
        /// </summary>
        /// <returns></returns>
        public IList<MasterDataCategoryDTO> GetMasterDataSort()
        {
            return _masterDataCategoryRepository.GetList()
                .To<MasterDataCategory, MasterDataCategoryDTO>().ToList();
        }

        #endregion

        #region 获取分页数据

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pf"></param>
        /// <returns></returns>
        public PagedResult<MasterDataDTO> MasterDataGetPaged(PagingFeature pf)
        {
            var paged = _masterDataRepository.Pagging(pf, null, "CODE ASC");

            return paged.To<MasterData, MasterDataDTO>();
        }

        #endregion

        #region 获取省份数据

        /// <summary>
        /// 获取省份数据
        /// </summary>
        /// <returns></returns>
        public IList<MasterDataDTO> GetProvinceData()
        {
            return _masterDataRepository
                .GetList(x => x.ParentCode == null, "Code ASC", null, new string[] { }).To<List<MasterDataDTO>>();
        }

        #endregion
        #endregion


        #region 主数据分类

        #region 保存主数据分类

        /// <summary>
        /// 保存主数据分类
        /// </summary>
        /// <param name="masterDataCategory">主数据分类</param>
        /// <returns>操作结果</returns>
        [Logging("保存主数据分类“{@p0.Name}”")]
        public Result SaveMasterDataCategory(MasterDataCategoryDTO masterDataCategory)
        {
            Result res = new Result();
            var info = masterDataCategory.To<MasterDataCategory>();
            if (masterDataCategory.Id.IsEmpty())
            {
                _masterDataCategoryRepository.Add(info);
            }
            else
            {
                _masterDataCategoryRepository.AutoUpdateFields(info).Update(info);
            }
            Context.Commit();

            return res;
        }

        #endregion

        #region 删除主数据分类

        /// <summary>
        /// 删除主数据分类
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Logging("删除主数据分类“{@p0}”")]
        public Result DeleteMasterDataCategory(int[] ids)
        {
            var res = new Result();
            if (ids == null)
            {
                res.Err = "Id参数不可为空！";
                return res;
            }

            _masterDataCategoryRepository.Remove(ids);
            _masterDataCategoryRepository.Context.Commit();

            return res;
        }

        #endregion

        #region 获取一条主数据分类

        /// <summary>
        /// 获取一条主数据分类
        /// </summary>
        /// <returns></returns>
        public MasterDataCategoryDTO GetMasterDataCategoryInfo(int id)
        {
            return _masterDataCategoryRepository.Get(id).To<MasterDataCategoryDTO>();
        }

        #endregion

        #region 获取主数据分类

        /// <summary>
        /// 获取主数据分类
        /// </summary>
        /// <returns></returns>
        public IList<MasterDataCategoryDTO> GetMasterDataCategory()
        {
            var data = _masterDataCategoryRepository.GetList()
                .To<List<MasterDataCategoryDTO>>();

            return data;
        }
        #endregion

        #endregion
    }
}